var datagrid = null;

var verifyConfigId = null;

$(function() {
	initDialog();
	initMsgDatagird();
	
	$("#updateVerifyConfigA").bind("click", function(){
		upsert = 1;
		verifyConfigId = null;
		MZ.clearForm("verifyConfigForm");
		var rowVals = datagrid.getRowValues();
		//reset
		if(rowVals.length == 1){
			rowVal = rowVals[0];
			verifyConfigId = rowVal.id;
			$("#configName").val(rowVal.verifyName);
			$("#description").val(rowVal.description);
			$("#ui-dialog-title-deptDialog").text("修改审核");
			MZ.openDialog("verifyConfigDialog");
		}else{
			MZ.showMsg("请选择一行记录修改!");
		}
		
	});
	
	$("#submitBtn").bind("click", function(){
		updateConfig();
	});
	
	$("#resetBtn").bind("click", function(){
		MZ.clearForm("verifyConfigForm");
	});
	
	$('#addOtherField').bind('click',function(){
		var liSize = $("#levelField li").length;
		var otherField=$('#otherFieldLI1').clone(true);
		otherField.attr("id","otherFieldLI"+liSize);
		otherField.insertBefore('#addOtherFieldLI');
		$("#otherFieldLI"+liSize+" span[id='levelSPAN']").text(liSize);
		$('#levelField li a[name="delOther"]').each(function(i){
			$("#otherFieldLI"+(i+1)+" a[name='delOther']").hide();
		});
		$("#otherFieldLI"+liSize+" a[name='delOther']").show();
		otherField.show();
	});
	
	$('#levelField a[name="delOther"]').bind('click',function(){
		var otherCost=$(this).parents("li");
		otherCost.remove();
		var liSize = $("#levelField li").length-1;
		if(liSize != 1){
			$("#otherFieldLI"+liSize+" a[name='delOther']").show();
		}
	});
});

function initDialog(){
	//DIALOG//
	$('#verifyConfigDialog').dialog({
		autoOpen: false,
		width: 500,
		height: 400,
		modal: true
	});
}

//init message datagird
//get json data from server
function initMsgDatagird(){
	//var width = $("#searchBox").width()-0;
	datagrid = $("#msgDatagird").flexigrid({
		url: "verifyconfig/getVerifyConfig",
		colModel : [
			{display: "ID", name : "id", width : 90, sortable : true, align: "center", hide: true},
			{display: "审核ID", name : "verifyCode", width : 150, sortable : true, align: "center", hide: true},
			{display: "审核名称", name : "verifyName", width : 150, sortable : true, align: "center"},
			{display: "审核角色", name : "roleId", width : 240, sortable : true, align: "center",  process:formatLevel},
			{display: "备注", name : "description", width : 600, sortable : true, align: "left"}
		]
    });
}

//funciton onSelectRow required
//update flexigrid onselectrow function
//if you need't use onselect row
//let this function blank to do nothing
function onSelectRow(tr){
	
}

function formatLevel(tdDiv, pid){
	var levelStr = "";
	var levelData = $(tdDiv).text();
	if($.trim(levelData) != ""){
		levelData = eval( "(" + levelData + ")" );
		var obj = null;
		for(var i in levelData){
			obj = levelData[i];
	 		levelStr+= "Level"+obj["level"]+":"+obj["name"]+",";
		} 
	}
	return levelStr;
}


function updateConfig(){
	var configName = $("#configName").val();
	if($.trim(configName)=="") {
		MZ.showMsg("审核名称不能为空!");
		return false;
	}
	var levelDataArr = new Array();
	var roleName = "";
	$("#levelField select[name='levelRoleSel']").each(function(i){
		if($(this).val() == null || $(this).val() == ""){
			MZ.showMsg("审核角色不能为空!");
			return false;
		}
		roleName = $(this).find("option:selected").text();
		levelDataArr.push("{'level':"+(i+1)+",'id':"+$(this).val()+",'name':'"+roleName+"'}");
	});
	var params = $("#verifyConfigForm").formSerialize()+"&configId="+verifyConfigId+"&levelData=["+levelDataArr.toString()+"]";
	$.ajax({
		url : "verifyconfig/updateVerifyConfig",
		// processData: false,
		type : "post",
		data : params,
		beforeSend : function() {
		},
		success : function(result) {
			if(result.succeed){
				MZ.reloadDatagrid("msgDatagird");
			}else{
				MZ.showMsg(result.message);
			}
			MZ.closeDialog("verifyConfigDialog");
		},
		error: function(result){
			MZ.showMsg(result);
		}
	});
}
